package com.example.service;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import com.neo.JpaThymeleafApplication;
import com.neo.entity.Order;
import com.neo.util.ReadFromFile;

@RunWith(SpringRunner.class)
@SpringBootTest(classes=JpaThymeleafApplication.class)
public class UpdateUserName2 {

	private static final Logger logger = LoggerFactory.getLogger(UpdateUserName2.class);
	@Autowired
    private JdbcTemplate jdbcTemplate;
	public static void main(String[] args) throws FileNotFoundException {
		
		new UpdateUserName2().WriteStringToFile("d:/123.txt");
	}
	
	public void WriteStringToFile(String filePath) {
        try {
            File file = new File(filePath);
            PrintStream ps = new PrintStream(new FileOutputStream(file));
            ps.println("http://www.jb51.net");// 往文件里写入字符串
            ps.append("11http://www.jb52.net");// 在已有的基础上添加字符串
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
	}  
	//jdbc 批量更新
	@Test
	public void batchUpdate() throws FileNotFoundException{
		//File file = new File("temp/1.sql");
        //PrintStream ps = new PrintStream(new FileOutputStream(file));
        
		List listCsv = ReadFromFile.readFileByLines("D:/点金石/债权12.12.csv");
    	for (int i = 1; i < listCsv.size(); i++) {//test
			String[] s = (String[])listCsv.get(i);
			String idNo = s[3];
			String mobileNo = s[4];
			
	    	String st =" UPDATE inv_user SET  mobile='"+mobileNo+"' where identity_no ='"+idNo+"';\n";
	    	
	    	System.out.println(st);
            //ps.println(st);// 往文件里写入字符串
	    	//ps.append(st);
		}
    	//long start = System.currentTimeMillis();

//		jdbcTemplate.batchUpdate(updateSql,new BatchPreparedStatementSetter() {
//            @Override
//            public void setValues(PreparedStatement preStatement, int i) throws SQLException {
//                preStatement.setString(1, orders.get(i).getUsername());
//                preStatement.setString(2, orders.get(i).getLoanid());
//            }
//            @Override
//            public int getBatchSize() {
//                return 1000;
//            }
//        });
//    	System.out.println("============"+(System.currentTimeMillis()-start)/1000);
//		String updateSql2 = "UPDATE user_base a SET  a.account_id=?  WHERE user_id = (select user_id from order_base where loan_id =? limit 1) ";
//		jdbcTemplate.batchUpdate(updateSql2,new BatchPreparedStatementSetter() {
//            @Override
//            public void setValues(PreparedStatement preStatement, int i) throws SQLException {
//                preStatement.setString(1, orders.get(i).getUsername());
//                preStatement.setString(2, orders.get(i).getLoanid());
//            }
//            @Override
//            public int getBatchSize() {
//                return 1000;
//            }
//        });
//    	System.out.println("============"+(System.currentTimeMillis()-start)/1000);
	}
	
	

}